const router = require("koa-router")();
const {
  addChatMessage,
  getMessageList,
  addGroupChatMessage,
  getGroupChatList,
} = require("../controller/chats");

router.prefix("/chats");

router.post("/postMessage", async (ctx) => {
  const dataObj = ctx.request.body;
  const result = await addChatMessage(dataObj);
  if (result) {
    ctx.body = {
      code: 0,
      data: result,
      msg: "发送消息成功!!",
    };
  } else {
    ctx.body = {
      code: 2,
      data: false,
      msg: "消息发送失败",
    };
  }
});
// 群聊消息
router.post("/postGroupChatMessage", async (ctx) => {
  const dataObj = ctx.request.body;

  const result = await addGroupChatMessage(dataObj);
  if (result) {
    ctx.body = {
      code: 0,
      data: true,
      msg: "发送消息成功!!",
    };
  } else {
    ctx.body = {
      code: 2,
      data: false,
      msg: "消息发送失败",
    };
  }
});
router.post("/getMessageList", async (ctx) => {
  const dataObj = ctx.request.body;
  const result = await getMessageList(dataObj);
  if (result) {
    ctx.body = {
      code: 0,
      data: result,
      msg: "获取消息成功!!",
    };
  } else {
    ctx.body = {
      code: 2,
      data: false,
      msg: "消息获取失败",
    };
  }
});
router.get("/getGroupChatList", async (ctx) => {
  const dataObj = ctx.request.query;
  const result = await getGroupChatList(dataObj);
  if (result) {
    ctx.body = {
      code: 0,
      data: result,
      msg: "获取消息成功!!",
    };
  } else {
    ctx.body = {
      code: 2,
      data: false,
      msg: "消息获取失败",
    };
  }
});

module.exports = router;
